package com.hitqz.robot.biz.flow.controller;

import com.hitqz.robot.biz.flow.service.LiteFlowService;
import com.hitqz.robot.biz.flow.service.XFlowService;
import com.hitqz.robot.biz.flow.domain.common.ResponseResult;
import com.hitqz.robot.biz.flow.domain.dto.SaveRuleDto;
import com.hitqz.robot.biz.flow.domain.json.xFlowRule.XFlowRuleJsonRootBean;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 流程控制器
 *
 * @author wengyx
 * @date 2023/08/22
 */
@RestController
@Slf4j
@RequestMapping("/flow/drawer")
public class WebController {

    @Resource
    private LiteFlowService liteFlowService;
    @Resource
    private XFlowService xFlowService;

    @PostMapping("/rule/parseFromJson")
    public ResponseResult<?> parseRuleFromJson(@RequestBody XFlowRuleJsonRootBean dto) {
        return xFlowService.parseRuleJson(dto);
    }

    @PostMapping("/rules")
    public ResponseResult<?> save(@RequestBody SaveRuleDto dto) {
        xFlowService.save(dto);
        return ResponseResult.success();
    }

    @GetMapping("/rules")
    public ResponseResult<?> getRulesList() {
        return ResponseResult.success(liteFlowService.getChainsList());
    }

    @GetMapping("/rules/{name}")
    public ResponseResult<?> getRule(@PathVariable(name = "name")String chainId) {
        return ResponseResult.success(liteFlowService.getChain(chainId));
    }

    @DeleteMapping("/rules/{name}")
    public ResponseResult<?> delete(@PathVariable(name = "name")String chainId) {
        liteFlowService.removeChain(chainId);
        return ResponseResult.success();
    }

    @PostMapping("/rules/deploy")
    public ResponseResult<?> deploy(@RequestBody SaveRuleDto dto) {
        return liteFlowService.setChain(dto.getChainId(),dto.getEl());
    }


    @GetMapping("/nodes")
    public ResponseResult<?> getNodePanelList() {
        return ResponseResult.success(liteFlowService.getNodePanelList());
    }

    @GetMapping("/rule/execute")
    public ResponseResult<?> execute(@RequestParam(name = "name")String chainId, @RequestParam(name = "cmd")Integer cmd) {
        return liteFlowService.executeChain(chainId, cmd);
    }




}
